/** @format */

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import * as dotenv from 'dotenv';
const { resolve } = require('path');
import fs from 'fs';
export default defineConfig(({ mode }) => {
  let envFile = `.env.${process.env.DEPLOY_ENV}`;
  const localEnvFile = `${envFile}.local`;
  if (fs.existsSync(resolve(__dirname, 'config/env', localEnvFile))) {
    envFile = localEnvFile;
  }
  envFile = fs.existsSync(resolve(__dirname, 'config/env', envFile))
    ? envFile
    : '.env.development';
  dotenv.config({ path: resolve(__dirname, 'config/env', envFile) });
  console.log('envFile', process.env.DEPLOY_ENV == 'development');
  return {
    plugins: [vue()],
    define: {
      __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'false',
    },
    resolve: {
      api: 'modern-compiler', // 使用了现代编译器 API 来处理 SCSS 文件，否则控制台会报错
      alias: [
        {
          find: '@',
          replacement: resolve(__dirname, 'src'),
        },
      ],
    },
    server: {
      host: '0.0.0.0',
      port: '4000',

      // headers: {
      //   'Content-Security-Policy':
      //     'frame-ancestors https://payapp.weixin.qq.com',
      //   'X-Frame-Options': 'ALLOW-FROM https://payapp.weixin.qq.com',
      // },
    },
    lintOnSave: false,
    publicPath: '/web/',
    /* 打包配置 */
    base: process.env.DEPLOY_ENV == 'development' ? '/' : '/web/',
    build: {
      brotliSize: false,
      emptyOutDir: false,
      outDir: 'web',
      assetsDir: 'static',
    },
    css: {
      preprocessorMaxWorkers: true,
      preprocessorOptions: {
        scss: {
          silenceDeprecations: ['legacy-js-api'],
          api: 'modern-compiler',
          additionalData: `@use "@/assets/css/global.scss" as *;`,
        },
      },
    },
  };
});
